package designpattern.command.llm.console;

import java.util.Scanner;
import designpattern.command.llm.command.CommandExec;

public class EditorConsole {
    private final CommandExec editor;
    
    public EditorConsole(CommandExec editor) {
        this.editor = editor;
        this.scanner = new Scanner(System.in);

        // 设置错误处理器
        this.editor.setErrorHandler(error -> System.out.println("! 错误: " + error));
    }

    private final Scanner scanner;
    
    public void start() {
        System.out.println("字符串编辑器已启动，输入 'exit' 退出");
        System.out.println("支持的命令：");
        System.out.println("- 追加: a 'string'");
        System.out.println("- 插入: i pos 'string'");
        System.out.println("- 删除: d pos len");
        System.out.println("- 显示当前内容: l");
        System.out.println("- 撤销: undo");
        System.out.println("- 重做: redo");

        while (true) {
            System.out.print("> ");
            String input = scanner.nextLine().trim();
            
            if (input.equals("exit")) {
                break;
            }
            
            processCommand(input);
        }
        
        scanner.close();
        System.out.println("编辑器已退出");
    }
    
    private void processCommand(String input) {
        String[] parts = input.split(" ", 3);
        String command = parts[0];
        
        try {
            switch (command) {
                case "a":
                    if (parts.length < 2) {
                        System.out.println("语法错误: a 'string'");
                        break;
                    }
                    String appendText = extractString(parts[1]);
                    editor.append(appendText);
                    break;
                    
                case "i":
                    if (parts.length < 3) {
                        System.out.println("语法错误: i pos 'string'");
                        break;
                    }
                    int insertPos = Integer.parseInt(parts[1]);
                    String insertText = extractString(parts[2]);
                    editor.insert(insertPos, insertText);
                    break;
                    
                case "d":
                    if (parts.length < 3) {
                        System.out.println("语法错误: d pos len");
                        break;
                    }
                    int deletePos = Integer.parseInt(parts[1]);
                    int deleteLen = Integer.parseInt(parts[2]);
                    editor.delete(deletePos, deleteLen);
                    break;
                    
                case "l":
                    System.out.println("当前内容: [" + editor.getContent() + "]");
                    break;
                    
                case "undo":
                    editor.undo();
                    break;
                    
                case "redo":
                    editor.redo();
                    break;
                    
                default:
                    System.out.println("未知命令: " + command);
            }
        } catch (NumberFormatException e) {
            System.out.println("数字格式错误");
        } catch (Exception e) {
            System.out.println("执行命令时出错: " + e.getMessage());
        }
    }
    
    // 从带引号的字符串中提取内容
    private static String extractString(String quoted) {
        if (quoted.startsWith("'") && quoted.endsWith("'")) {
            return quoted.substring(1, quoted.length() - 1);
        }
        return quoted;
    }
    
    public static void main(String[] args) {
        CommandExec editor = new CommandExec();
        EditorConsole console = new EditorConsole(editor);
        console.start();
    }
}